﻿using Dapper;
using OMG.Fill.Mulberry.Model;
using OMG.Fill.Mulberry.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Linq;
namespace OMG.Fill.Mulberry.Service
{
    /// <summary>
    /// 操作日志T_Logs通用操作类
    /// </summary>
    public class LogsServices:Logss
    {
        public Pager<List<LogsModel>> GetPageList(LogsQuery query)
        {
            Pager<List<LogsModel>> obj = new Pager<List<LogsModel>>();
            var paramlist = new DynamicParameters();
            string strWhereSql = "";//from 表在下面，需完善其他查询条件在这里接的写，格式为："and xxx=@xxx空格,如下注释部分代码所示："
            if (!string.IsNullOrEmpty(query.ActionName))
            {
                strWhereSql += "and ActionName like @ActionName ";
                paramlist.Add("ActionName", "%" + query.ActionName + "%");
            }
            if (!string.IsNullOrEmpty(query.ActionCode))
            {
                strWhereSql += "and ActionCode = @ActionCode ";
                paramlist.Add("ActionCode", query.ActionCode);
            }
            if (!string.IsNullOrEmpty(query.Account))
            {
                strWhereSql += "and Account = @Account ";
                paramlist.Add("Account", query.Account);
            }
            if (!string.IsNullOrEmpty(query.LogsTime.ToString()))
            {
                strWhereSql += "and LogsTime > @LogsTime ";
                paramlist.Add("LogsTime", query.LogsTime);
            }
            if (!string.IsNullOrEmpty(query.IpAddress))
            {
                strWhereSql += "and IpAddress like @IpAddress ";
                paramlist.Add("IpAddress", "%" + query.IpAddress + "%");
            }
            string strFromSql = "from dbo.T_Logs where 1=1 " + strWhereSql;
            string strTotal = "select count(1)" + strFromSql + strWhereSql;
            string strOrderSql = "";//自定义完成order by语句
            string strSql = @"select * from (select *,ROW_NUMBER() over (order by Id ) as RowNumber " + strFromSql + ") as t ";
            strSql += "where t.RowNumber between  @minRow and @maxRow" + strOrderSql;
            paramlist.Add("minRow", query.MinRow);
            paramlist.Add("maxRow", query.MaxRow);
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.rows = dbCon.Query<LogsModel>(strSql,paramlist).ToList();
                    foreach (var item in obj.rows)
                    {
                        item.LogsTimeString = item.LogsTime.ToString();
                    }
                    obj.total = dbCon.Query<int>(strTotal,paramlist).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取操作日志T_Logs分页列表失败", ex);
                return obj;
            }
        }
    }
}